package com.zgl.modules.card.controller;

import com.zgl.modules.card.DTO.CardDTO;
import com.zgl.modules.card.entity.CardEntity;
import com.zgl.modules.card.service.impl.CardServiceImpl;
import com.zgl.modules.goods.DTO.GoodsDTO;
import com.zgl.modules.goods.entity.GoodsEntity;
import com.zgl.modules.user.entity.User;
import com.zgl.modules.user.service.UserService;
import com.zgl.utils.Params;
import com.zgl.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@Api(tags = "银行卡操作")
@RequestMapping("/card")
public class CardController {
    @Resource
    private CardServiceImpl cardService;

    @PostMapping("/bindcard")
    @ApiOperation(value = "绑定银行卡")
    @ResponseBody
    //,@CookieValue(value = "token",
    //                     defaultValue = "null") String headerToken
    public R saveOne(@RequestBody Params<CardDTO> params) {
//        final String headerToken = cardDTO.getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(params.getParams().getToken()).getBody();
        String tokenUserId = (String) claims.get("userid");
        CardEntity cardEntity = new CardEntity();
        BeanUtils.copyProperties(params.getParams(), cardEntity);
        CardEntity myCardEntity=(CardEntity)cardService.getCardByUserNumber(cardEntity.getCardNumber()).getData();
        if (myCardEntity!=null) {
            if (myCardEntity.getUsed() == 1) {
                return R.error("此银行卡已被其他用户绑定，请先解除绑定");
            }
            myCardEntity.setUserid(tokenUserId);
            return cardService.modifyOne(cardEntity);
        }
        cardEntity.setUsed(1);
        cardEntity.setCardBalance("10000");
        cardEntity.setUserid(tokenUserId);
        return cardService.savaOne(cardEntity);
    }

    @PostMapping("/removeOne")
    @ApiOperation(value = "删除银行卡根据银行卡id")
    @ResponseBody
    public R removeOne(@RequestBody CardDTO cardDTO) {
        CardEntity cardEntity = new CardEntity();
        BeanUtils.copyProperties(cardDTO, cardEntity);
        cardEntity.setUsed(0);
        return cardService.modifyOne(cardEntity);
    }

    @PostMapping("/modifyOne")
    @ApiOperation(value = "修改银行卡")
    @ResponseBody
    public R modifyOne(@RequestBody CardDTO cardDTO) {
        CardEntity cardEntity = new CardEntity();
        BeanUtils.copyProperties(cardDTO, cardEntity);
        return cardService.modifyOne(cardEntity);
    }

    @PostMapping("/getCardByUserId")
    @ApiOperation(value = "根据用id查找该用户下绑定的的银行")
    @ResponseBody
    public R getCardByUserId(@RequestBody Params<CardDTO> params) {
        final String headerToken = params.getParams().getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(headerToken).getBody();
        String tokenUserId = (String) claims.get("userid");
        return cardService.getCardByUserId(tokenUserId);
    }

}
